View Javadoc

1   /*
2    * The ReWinder Project
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8   
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13  
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  package ch.twiddlefinger.inet.rewinder.view;
19  
20  import java.awt.BorderLayout;
21  import java.awt.Color;
22  import java.awt.Container;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.net.MalformedURLException;
29  import java.util.Iterator;
30  
31  import javax.swing.JButton;
32  import javax.swing.JFileChooser;
33  import javax.swing.JFrame;
34  import javax.swing.JLabel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTextArea;
37  
38  import ch.twiddlefinger.inet.rewinder.model.entities.Player;
39  import ch.twiddlefinger.inet.rewinder.model.parser.ReplayParser;
40  import ch.twiddlefinger.inet.rewinder.model.parser.ReplayParserFactory;
41  import ch.twiddlefinger.inet.rewinder.model.replay.WarcraftReplay;
42  import ch.twiddlefinger.inet.rewinder.resource.Resource;
43  
44  public class RewinderMainView extends JFrame implements ActionListener {
45  	
46  	/*** FileChooser Object to select files */
47      private JFileChooser fc = new JFileChooser();
48      
49      /*** Output Area */
50      private JTextArea out;
51      
52      /*** Browse Button for searching for replays */
53      private JButton browseButton;
54  
55      public RewinderMainView() {
56          super("ReWinder");
57          this.setSize(600, 500);
58          this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
59          this.initComponents();
60          this.show();
61      }
62  
63      /***
64       * Initialisation of GUI components
65       *
66       */
67      private void initComponents() {
68          Container cp = this.getContentPane();
69          browseButton = new JButton("Browse for a Replay...");
70          browseButton.addActionListener(this);
71          out = new JTextArea();
72          out.setForeground(Color.WHITE);
73          out.setBackground(Color.BLACK);
74          cp.setLayout(new BorderLayout());
75          cp.add(browseButton, BorderLayout.SOUTH);
76          cp.add(new JScrollPane(out), BorderLayout.CENTER);
77          cp.add(new JLabel(Resource.getIcon("banner.jpg")), BorderLayout.NORTH);
78          cp.validate();
79      }
80  
81  	/***
82  	 * Catch and parse after te user has opened a file
83  	 */
84      public void actionPerformed(ActionEvent e) {
85          if (e.getSource().equals(browseButton)) {
86              int returnVal = fc.showOpenDialog(this);
87  
88              if (returnVal == JFileChooser.APPROVE_OPTION) {
89                  out.append("Trying to parse..." +
90                      fc.getSelectedFile().getName() + "\n");
91  
92                  File file = fc.getSelectedFile();
93                  InputStream in = null;
94  
95                  try {
96                      in = (file.toURL()).openStream();
97                  } catch (MalformedURLException e1) {
98                      out.append("Error, this file does not exist!");
99                  } catch (IOException e1) {
100                     out.append("Something went wrong, couldn't read the file.");
101                 }
102 
103                 if (in != null) {
104                     ReplayParserFactory factory = new ReplayParserFactory();
105                     ReplayParser parser = factory.getInstance();
106 
107                     try {
108                         WarcraftReplay rep = parser.parse(in);
109                         out.setText("");
110                         this.printReplayInfo(rep, file.getName());
111                         ;
112                     } catch (IllegalArgumentException e1) {
113                         out.append("That was probably not a replay, jerk!\n");
114                     }
115                 }
116             } else {
117                 out.append("User canceled selection of replay...");
118             }
119         }
120     }
121 
122     /***
123      * Prints infos about a replay to the console.
124      *
125      * @param rep
126      */
127     public void printReplayInfo(WarcraftReplay rep, String repName) {
128         // TODO Silent tests
129         Iterator players = rep.getPlayerIterator();
130         out.append("\n\n********* Replay Info " + repName + " ***********" +
131             "\n");
132 
133         out.append(rep.getGameType() + "\n");
134         int minutes = (int) (rep.getLengthMsec() / 1000 / 60);
135         int seconds = (int) ((((double)rep.getLengthMsec() / 1000 / 60) - minutes) * 60);
136         
137         out.append("Length: " + minutes + ":" + seconds  + " minutes\n");
138         out.append("Game version: " + rep.getVersion());
139 
140         out.append("\n********** Players ***********\n");
141 
142         while (players.hasNext()) {
143             Player p = (Player) players.next();
144             out.append(p.getName() + "\t"+  p.getRace() + "\n");
145         }
146 
147         out.append("\n********** Teams ***********\n");
148 
149         Iterator teams = rep.getTeamIterator();
150 
151         while (teams.hasNext()) {
152             out.append(teams.next() + "\n");
153         }
154     }
155 }